<?php
/**
 * 买家两个小时内没有付款的取消订单，定时任务10分钟执行一次.
 * User: admin
 * Date: 2019/10/15
 * Time: 16:41
 */

if (!defined("APP_PATH")) {
    define('APP_PATH', __DIR__ . '/application/');
    // 加载框架引导文件
    require __DIR__ . '/thinkphp/start.php';
}

echo '开始执行时间：'.date('Y-m-d H:i:s')."\r\n";

//获取两个小时内没有付款的订单
$data = db('buy_order')->where('status',1)->where('createtime','<',time()-7200)->limit(0,1000)->field('id,uid,username,sell_id,sell_fee,sell_uid,sell_username,num,price,total_money')->select();
$prefix = config('database.prefix');
$text = config('site.credit1_text');
$text2 = config('site.credit2_text');
$db = new \think\Db();
$tm = time();
while ($data) {
    try {
        $db::startTrans();
        $user = null;
        $log = null;
        $buyorder = null;
        $sellorder = null;
        foreach ($data as $val) {
            //买家返回usdt
            $user[] = 'update '.$prefix.'user set credit2 = credit2 + '.floatval($val['total_money']).',lock_credit2 = lock_credit2 - '.floatval($val['total_money']).' where id = '.intval($val['uid']);
            //卖家返回fc
            $user[] = 'update '.$prefix.'user set credit1 = credit1 + '.floatval($val['num']+$val['sell_fee']).',lock_credit1 = lock_credit1 - '.floatval($val['num']+$val['sell_fee']).' where id = '.intval($val['sell_uid']);
            $log[] = [
                'username'   => $val['username'],
                'type'       => 'credit2',
                'project'    => 'lockmoney',
                'num'        => floatval($val['total_money']),
                'remark'     => '两小时没付款，取消订单返回冻结的'.$text2,
                'remark_en'  => 'Two hours without payment, cancel order and return to frozen '.$text2,
                'createtime' => $tm,
                'updatetime' => $tm
            ];
            $log[] = [
                'username'   => $val['sell_username'],
                'type'       => 'credit1',
                'project'    => 'lockmoney',
                'num'        => floatval($val['num']+$val['sell_fee']),
                'remark'     => '买家两小时没付款，取消订单返回冻结的'.$text,
                'remark_en'  => 'The buyer did not pay for two hours, cancelled the order and returned to the freeze '.$text,
                'createtime' => $tm,
                'updatetime' => $tm
            ];
            $buyorder[] = 'update '.$prefix.'buy_order set status = -1 where id = '.intval($val['id']);
            $sellorder[] = 'update '.$prefix.'sell_order set status = -1 where id = '.intval($val['sell_id']);
        }
        //批量处理
        if ($user) {
            db()->batchQuery($user);
            $user = null;
        }
        if ($buyorder) {
            db()->batchQuery($buyorder);
            $buyorder = null;
        }
        if ($sellorder) {
            db()->batchQuery($sellorder);
            $sellorder = null;
        }
        if ($log) {
            db('cc_detail_log')->insertAll($log);
            $log = null;
        }
        $db::commit();
        $data = db('buy_order')->where('status',1)->where('createtime','<',time()-7200)->limit(0,1000)->field('id,uid,username,sell_id,sell_fee,sell_uid,sell_username,num,price,total_money')->select();
    } catch (\Exception $e) {
        //事务回滚
        $db::rollback();
        //记录日志
        plog('买单两小时没付款',$e->getMessage());
        //跳出循环
        break;
    }
}
echo '执行结束时间：'.date('Y-m-d H:i:s')."\r\n";